package player;

import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.effect.Reflection;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import vk.Video;

import java.util.ArrayList;
import java.util.List;

/**
 * Date: 24.11.12
 *
 * @author Mikhail Larinskiy
 */
public class Player extends Parent {
    private final ImageView view = new ImageView();
    private final Video video;

    public Player(Video video) {
        this.video = video;
        Image image = new Image(this.video.getThumb());
        view.setImage(image);
        getChildren().add(view);

        Reflection reflection = new Reflection();
        view.setEffect(reflection);
        reflection.setBottomOpacity(0);
        reflection.setTopOpacity(0.66);
        reflection.setFraction(0.29);
        reflection.setTopOffset(-10);
    }

    public void setSize(double width, double height) {
        view.resize(width, height);
        view.autosize();
        autosize();
    }

    public void playPause() {
        WebView webView = new WebView();
        final WebEngine engine = webView.getEngine();
        engine.load(video.getPlayer());
        Stage stage = new Stage();
        Group root = new Group();
        root.getChildren().add(webView);
        stage.setScene(new Scene(root));
        stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            public void handle(WindowEvent windowEvent) {
                engine.load("");
            }
        });
        stage.show();
    }

    public static List<Player> videosToPlayerList(List<Video> videos) {
        List<Player> res = new ArrayList<Player>();
        for (Video video : videos) {
            res.add(new Player(video));
        }
        return res;
    }
}
